<?php

namespace app\common\model;


/**
 * Class app\common\model\Setting
 *
 * @property bool $is_view 是否显示 1=显示 2=不显示
 * @property bool $status 是否启用 1=启用 2=不启用
 * @property int $id
 * @property string $group 所属组
 * @property string $key 设置项key
 * @property string $remark 初始值
 * @property string $title 设置项名称
 * @property string $type 参数类型:'JSON','STRING','DATE','INT','FLOAT'
 * @property string $update_time
 * @property string $value 设置项值
 */
class Setting extends BaseModel
{
    // 表名
    protected $table = 'setting';

    // 追加属性
    protected $append = [

    ];

    protected $hidden = [
//        "remark",
        "status",
        "is_view",
        "group",
        "update_time",
    ];

    public function getValueAttr($value, $data)
    {
        $type = $data['type'];

        if (is_null($value)) {
            return null;
        }

        if (is_array($type)) {
            [$type, $param] = $type;
        } elseif (strpos($type, ':')) {
            [$type, $param] = explode(':', $type, 2);
        }

        switch ($type) {
            case 'integer':
            case 'int':
                $value = (int)$value;
                break;
            case 'string':
                $value = (string)$value;
                break;
            case 'float':
                if (empty($param)) {
                    $value = (float)$value;
                } else {
                    $value = (float)number_format($value, (int)$param, '.', '');
                }
                break;
            case 'boolean':
                $value = (bool)$value;
                break;
            case 'timestamp':
                $format = !empty($param) ? $param : $this->dateFormat;
                $value = $this->formatDateTime($format, $value, true);
                break;
            case 'datetime':
                $format = !empty($param) ? $param : $this->dateFormat;
                $value = $this->formatDateTime($format, $value);
                break;
            case 'json':
                $value = json_decode($value, true);
                break;
            case 'array':
                $value = empty($value) ? [] : json_decode($value, true);
                break;
            case 'object':
                $value = empty($value) ? new \stdClass() : json_decode($value);
                break;
            case 'serialize':
                try {
                    $value = unserialize($value);
                } catch (\Exception $e) {
                    $value = null;
                }
                break;
            default:
                if (str_contains($type, '\\')) {
                    // 对象类型
                    $value = new $type($value);
                }
        }
        return $value;
    }

    public function setValueAttr($value, $data)
    {
        $type = $data['type'];


        if (is_null($value)) {
            return null;
        }

        if (is_array($type)) {
            [$type, $param] = $type;
        } elseif (strpos($type, ':')) {
            [$type, $param] = explode(':', $type, 2);
        }

        switch ($type) {
            case 'integer':
                $value = (int)$value;
                break;
            case 'string':
                $value = (string)$value;
                break;
            case 'float':
                if (empty($param)) {
                    $value = (float)$value;
                } else {
                    $value = (float)number_format($value, (int)$param, '.', '');
                }
                break;
            case 'boolean':
                $value = (bool)$value;
                break;
            case 'timestamp':
                $format = !empty($param) ? $param : $this->dateFormat;
                $value = $this->formatDateTime($format, $value, true);
                break;
            case 'datetime':
                $format = !empty($param) ? $param : $this->dateFormat;
                $value = $this->formatDateTime($format, $value);
                break;
            case 'json':
                $value = json_encode($value, JSON_UNESCAPED_UNICODE);
                break;
            case 'array':
                $value = empty($value) ? [] : json_decode($value, true);
                break;
            case 'object':
                $value = empty($value) ? new \stdClass() : json_encode($value, JSON_UNESCAPED_UNICODE);
                break;
            case 'serialize':
                try {
                    $value = serialize($value);
                } catch (\Exception $e) {
                    $value = null;
                }
                break;
            default:
                if (false !== strpos($type, '\\')) {
                    // 对象类型
                    $value = (string) new $type($value);
                }
        }
        return $value;
    }
}
